/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit.tasks;

import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree;
import java.util.Collections;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import org.netbeans.api.java.source.JavaSource.Phase;
import org.netbeans.api.java.source.Task;
import org.netbeans.api.java.source.TreeMaker;
import org.netbeans.api.java.source.WorkingCopy;
import org.openide.WizardDescriptor;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public class CloseConnectionTask implements Task<WorkingCopy> {

    WizardDescriptor wizard;

    public CloseConnectionTask(WizardDescriptor wizard) {
        this.wizard = wizard;
    }

    @Override
    public void run(WorkingCopy workingCopy) throws Exception {
        workingCopy.toPhase(Phase.RESOLVED); // is it neccessary?

        CompilationUnitTree cut = workingCopy.getCompilationUnit();
        TreeMaker make = workingCopy.getTreeMaker();

        for (Tree typeDecl : cut.getTypeDecls()) {
            if (Tree.Kind.CLASS == typeDecl.getKind()) {
                ClassTree clazz = (ClassTree) typeDecl;

                AnnotationTree annotationTree = make.Annotation(
                        make.Identifier("Override"),
                        Collections.<ExpressionTree>emptyList());

                ModifiersTree methodModifiers = make.Modifiers(
                        Collections.<Modifier>singleton(Modifier.PROTECTED),
                        Collections.<AnnotationTree>singletonList(annotationTree));

                VariableTree parameter = make.Variable(
                        make.Modifiers(
                        Collections.<Modifier>emptySet(),
                        Collections.<AnnotationTree>emptyList()),
                        "connection", // name
                        make.Identifier("IDatabaseConnection"), // parameter type
                        null // initializer - does not make sense in parameters.
                        );

                TypeElement element = workingCopy.getElements().getTypeElement("java.lang.Exception");
                ExpressionTree throwsClause = make.QualIdent(element);
                
                MethodTree newMethod = make.Method(
                        methodModifiers, 
                        "closeConnection",
                        make.PrimitiveType(TypeKind.VOID),
                        Collections.<TypeParameterTree>emptyList(), // type parameters - none
                        Collections.<VariableTree>singletonList(parameter), 
                        Collections.<ExpressionTree>singletonList(throwsClause), // throws
                        "{super.closeConnection(connection);}",
                        null // default value - not applicable here, used by annotations
                        );

                ClassTree modifiedClazz = make.addClassMember(clazz, newMethod);
                workingCopy.rewrite(clazz, modifiedClazz);
            }
        }
    }
}
